<template>
  <div>
    <div class="crud-group">
      <el-input
        v-model="paramsForm.groupSign"
        placeholder="标志"
      />
      <el-input
        v-model="paramsForm.groupSignDesc"
        placeholder="参数标志描述"
      />
      <el-input
        v-model="paramsForm.paramName"
        placeholder="参数名"
      />
      <el-input
        v-model="paramsForm.paramNameDesc"
        placeholder="参数名描述"
      />
      <el-input
        v-model="paramsForm.paramValue"
        placeholder="参数值"
      />
      <el-input
        v-model="paramsForm.sortOrder"
        placeholder="排序顺序"
      />
      <el-select
        v-model="paramsForm.mutable"
        placeholder="是否可修改"
      >
        <el-option 
          v-for="item in [{
            label: '是',
            value: 'Y'
          }, {
            label: '否',
            value: 'N'
          }]"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <el-input
        v-model="paramsForm.clusterName"
        placeholder="集群归属"
      />
      <el-button
        type="success"
        icon="el-icon-search"
        @click="getData"
      >搜索</el-button>
      <el-button
        type="warning"
        icon="el-icon-refresh-left"
        @click="reset"
      >重置</el-button>
      <el-button
        type="primary"
        icon="el-icon-plus"
        @click="()=>{
          selectedRow = null
          addDialogVisible = true
        }"
      >新增</el-button>
    </div>

    <el-table
      v-loading="isLoading"
      border
      :data="data"
    >
      <el-table-column
        label="标志"
        prop="groupSign"
      />
      <el-table-column
        label="参数标志描述"
        prop="groupSignDesc"
      />
      <el-table-column
        label="参数名"
        prop="paramName"
      />
      <el-table-column
        label="参数名描述"
        prop="paramNameDesc"
      />
      <el-table-column
        label="参数值"
        prop="paramValue"
      />
      <el-table-column
        label="排序顺序"
        prop="sortOrder"
      />
      <el-table-column
        label="是否可修改"
        prop="mutable"
      >
        <template slot-scope="scope">
          {{ scope.row.mutable === 'Y' ? '是' : '否' }}
        </template>
      </el-table-column>
      <el-table-column
        label="集群归属"
        prop="clusterName"
      />
      <el-table-column
        label="操作"
        width="100px"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="()=>{
              selectedRow = Object.assign({}, scope.row)
              addDialogVisible = true
            }
            "
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            @click="()=>{
              deleteRow(scope.row.id)
            }"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination
        layout="prev, pager, next,sizes,total"
        :page-sizes="[5, 10, 20, 30, 40]"
        :current-page="paramsForm.current"
        :page-size="paramsForm.size"
        :total="total"
        @size-change="(val)=>{
          paramsForm.current = 1
          paramsForm.size = val
          getData()
        }"
        @current-change="(val)=>{
          paramsForm.current = val
          getData(false)
        }"
      />
    </div>

    <AddConfigDialog
      :config="selectedRow"
      :dialog-visible="addDialogVisible"
      @close="()=>{
        addDialogVisible = false
        getData()
      }"
    />
  </div>
</template>

<script>
import AddConfigDialog from './components/AddConfigDialog.vue'
import coldApi from '@/api/coldStandby/index'

const form = {
  current: 1,
  size: 10,
  id: '',
  groupSign: '',
  groupSignDesc: '',
  paramName: '',
  paramNameDesc: '',
  paramValue: '',
  sortOrder: '',
  mutable: '',
  clusterName: ''
}
export default {
  components: {
    AddConfigDialog
  },
  data() {
    return {
      data: [],
      total: 0,
      isLoading: false,
      addDialogVisible: false,
      paramsForm: {...form},
      selectedRow: null
    }
  },
  created() {
    this.getData()
  },
  methods: {
    getData(resetPage = true) {
      this.isLoading = true
      if (resetPage) {
        this.paramsForm.current = 1
      }
      coldApi.cbParamConfig(this.paramsForm).then(res => {
        this.data = res.records
        this.total = res.total
      }).finally(() => {
        this.isLoading = false
      })
    },
    deleteRow(id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        coldApi.delCbParamConfig(id).then(() => {
          this.getData()
        
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    reset() {
      this.paramsForm = {...form}
      this.getData()
    }
  }
}
</script>

<style lang="scss" scoped>

</style>

